package com.ryl.yomall.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Date 20:25
 * @Author ryl
 */
@Configuration
public class DeadLetterMqConfig {
    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";

    /**
     * 其他队列可以在RabbitListener上面做绑定
     * @return
     */
    //普通队列
    @Bean
    public Queue queue2(){
        return  QueueBuilder.durable (queue_dead_2).build ();
    }
    //队列 TTL
    @Bean
    public Queue queue1(){
        Map<String, Object> arguments = new HashMap<> (  );
        arguments.put("x-dead-letter-exchange", exchange_dead);
        arguments.put("x-dead-letter-routing-key", routing_dead_2);
        //方式二，统一延迟时间
        //arguments.put("x-message-ttl", 10 * 1000);

        //方式一与方式二切换时，必须先删除对应交换机与队列，否则出错

        return QueueBuilder.durable ( queue_dead_1 ).withArguments ( arguments ).build ();
    }
    //交换机
    @Bean
    public Exchange exchange(){
       return ExchangeBuilder.directExchange ( exchange_dead ).build ();
    }
    //绑定
    @Bean
    public Binding binding1(){
        return BindingBuilder.bind ( queue1 () ).to ( exchange () ).with ( routing_dead_1 ).noargs ();
    }
    @Bean
    public Binding binding2(){
        return BindingBuilder.bind ( queue2 () ).to ( exchange () ).with ( routing_dead_2 ).noargs ();
    }

}
